<?php
namespace app\admin\controller;

use app\common\cache\car\ICarBrandCache;
use app\common\cache\car\ICarConfigCache;
use app\common\cache\car\ICarModelCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class Car extends AdminBaseController{

    /**
     * @功能：车辆品牌列表
     * @开发者：cxl
     * @return string
     */
    public function car_brand_list(){
        //接收参数
        $car_brand_id = IRequest::get('car_brand_id');
        $car_brand_name = IRequest::get('car_brand_name');
        $car_brand_name_py = IRequest::get('car_brand_name_py');

        //设置查询条件
        $car_brand_where = null;
        if(!empty($car_brand_id)) $car_brand_where['car_brand_id'] = $car_brand_id;
        if(!empty($car_brand_name)) $car_brand_where['car_brand_name'] = array("like","%{$car_brand_name}%");
        if(!empty($car_brand_name_py)) $car_brand_where['car_brand_name_py'] = array("like","%{$car_brand_name_py}%");
        $car_brand_list = IDb::getInstance('asset_car_brand')
             ->setDbWhere($car_brand_where)
            ->setDbOrder("car_brand_name_py")
            ->pag();

        //页面赋值
        $this->assign("car_brand_id",$car_brand_id);
        $this->assign("car_brand_name",$car_brand_name);
        $this->assign("car_brand_name_py",$car_brand_name_py);
        $this->assign("car_brand_list",$car_brand_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：车辆品牌详情
     * @开发者：cxl
     * @return string
     */
    public function car_brand_info(){
        //接收参数
        $car_brand_id = IRequest::get('car_brand_id',IRequest::NOT_EMPTY,"车辆品牌编号不能为空！");

        //设置查询条件
        $car_brand_info = ICarBrandCache::getInfo($car_brand_id);

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("car_brand_info",$car_brand_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加车辆品牌
     * @开发者：cxl
     * @return string
     */
    public function car_brand_add(){
        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑车辆品牌
     * @开发者：cxl
     * @return string
     */
    public function car_brand_edit(){
        //接收参数
        $car_brand_id = IRequest::get('car_brand_id',IRequest::NOT_EMPTY,"车辆品牌编号不能为空！");

        //设置查询条件
        $car_brand_info = ICarBrandCache::getInfo($car_brand_id);
        if(empty($car_brand_info['car_brand_id'])){
            error("取得车辆品牌信息失败！");
        }

        //页面赋值
        $this->assign("car_brand_id",$car_brand_id);
        $this->assign("car_brand_info",$car_brand_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑车辆品牌提交
     * 开发者：cxl
     */
    public function car_brand_submit(){
        //接收参数
        $car_brand_id = IRequest::get("car_brand_id");
        $car_brand_name = IRequest::get("car_brand_name",IRequest::NOT_EMPTY,"车辆品牌名称不能为空！");
        $car_brand_name_py = IRequest::get("car_brand_name_py",IRequest::NOT_EMPTY,"车辆品牌拼音不能为空！");
        $car_brand_url = IRequest::get("car_brand_url");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        //判断数据提交类型
        if(empty($car_brand_id)){
            //判断是否存在
            $car_brand_where['car_brand_name'] = $car_brand_name;
            $car_brand_info = IDb::getInstance("asset_car_brand")->setDbWhere($car_brand_where)->row();
            if(!empty($car_brand_info)){
                error("您输入的品牌已经存在！");
            }

            //新增保存
            $car_brand_data['car_brand_name'] = $car_brand_name;
            $car_brand_data['car_brand_name_py'] = $car_brand_name_py;
            $car_brand_data['car_brand_url'] = $car_brand_url;
            $car_brand_data['order_by'] = $order_by;
            $car_brand_data['remark'] = $remark;
            $car_brand_data['create_admin'] = get_login_admin_id();
            $car_brand_data['create_time'] = get_date_time();
            $car_brand_add = IDb::getInstance("asset_car_brand")->setDbData($car_brand_data)->add();
            if($car_brand_add === false){
                error("创建车辆品牌失败！");
            }
        }else{
            //修改保存
            $car_brand_data['car_brand_name'] = $car_brand_name;
            $car_brand_data['car_brand_name_py'] = $car_brand_name_py;
            $car_brand_data['car_brand_url'] = $car_brand_url;
            $car_brand_data['order_by'] = $order_by;
            $car_brand_data['remark'] = $remark;
            $car_brand_data['update_admin'] = get_login_admin_id();
            $car_brand_data['update_time'] = get_date_time();
            $car_brand_where['car_brand_id'] = $car_brand_id;
            $car_brand_upd = IDb::getInstance("asset_car_brand")->setDbWhere($car_brand_where)->setDbData($car_brand_data)->upd();
            if($car_brand_upd === false){
                error("修改车辆品牌失败！");
            }

            //删除缓存
            ICarBrandCache::deleteInfo($car_brand_id);
        }

        //删除缓存
        ICarBrandCache::deleteList();

        //画面跳转
        $this->redirect("car_brand_list");
    }

    /**
     * @功能：删除汽车品牌
     * @开发者：cxl
     * @return string
     */
    public function car_brand_del(){
        //接收参数
        $car_brand_id = IRequest::get('car_brand_id',IRequest::NOT_EMPTY,"车辆品牌编号不能为空！");

        //修改管理员信息
        $car_brand_where['car_brand_id'] = $car_brand_id;
        $car_brand_data['is_delete'] = IsDelete::Yes;
        $car_brand_data['update_admin'] = get_login_admin_id();
        $car_brand_data['update_time'] = get_date_time();
        $car_brand_upd = IDb::getInstance('asset_car_brand')->setDbWhere($car_brand_where)->setDbData($car_brand_data)->upd();
        if($car_brand_upd === false){
            error("删除汽车品牌失败！");
        }

        //删除缓存
        ICarBrandCache::deleteInfo($car_brand_id);
        ICarBrandCache::deleteList();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：车辆品牌还原
     * @开发者：cxl
     * @return string
     */
    public function car_brand_recycle(){
        //接收参数
        $car_brand_id = IRequest::get('car_brand_id',IRequest::NOT_EMPTY,"车辆品牌编号不能为空！");

        //修改管理员信息
        $car_brand_where['car_brand_id'] = $car_brand_id;
        $car_brand_data['is_delete'] = IsDelete::No;
        $car_brand_data['update_admin'] = get_login_admin_id();
        $car_brand_data['update_time'] = get_date_time();
        $car_brand_upd = IDb::getInstance('asset_car_brand')->setDbWhere($car_brand_where)->setDbData($car_brand_data)->upd();
        if($car_brand_upd === false){
            error("车辆品牌还原失败！");
        }

        //删除缓存
        ICarBrandCache::deleteInfo($car_brand_id);
        ICarBrandCache::deleteList();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：车辆型号列表
     * @开发者：cxl
     * @return string
     */
    public function car_model_list(){
        //接收参数
        $car_brand_id = IRequest::get('car_brand_id');
        $car_model_id = IRequest::get('car_model_id');
        $car_model_name = IRequest::get('car_model_name');

        //设置查询条件
        $car_brand_list = ICarBrandCache::getList();

        //设置查询条件
        $car_model_where = null;
        if(!empty($car_brand_id)) $car_model_where['car_brand_id'] = $car_brand_id;
        if(!empty($car_model_id)) $car_model_where['car_model_id'] = $car_model_id;
        if(!empty($car_model_name)) $car_model_where['car_model_name'] = array("like","%{$car_model_name}%");
        $car_model_list = IDb::getInstance('asset_car_model')
            ->setDbWhere($car_model_where)
            ->setDbOrder("car_model_id")
            ->pag();

        //页面赋值
        $this->assign("car_brand_id",$car_brand_id);
        $this->assign("car_model_id",$car_model_id);
        $this->assign("car_model_name",$car_model_name);
        $this->assign("car_brand_list",$car_brand_list);
        $this->assign("car_model_list",$car_model_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：车辆品牌型号详情
     * @开发者：cxl
     * @return string
     */
    public function car_model_info(){
        //接收参数
        $car_model_id = IRequest::get('car_model_id',IRequest::NOT_EMPTY,"车辆品牌编号不能为空！");

        //设置查询条件
        $car_model_info = ICarModelCache::getInfo($car_model_id);
        if(empty($car_model_info['car_brand_id'])){
            error("取得汽车品牌型号信息失败！");
        }

        //设置查询条件
        $car_brand_info = ICarBrandCache::getInfo($car_model_info['car_brand_id']);
        if(empty($car_brand_info['car_brand_id'])){
            error("取得汽车品牌型号信息失败2！");
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("car_brand_info",$car_brand_info);
        $this->assign("car_model_info",$car_model_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加车辆型号
     * @开发者：cxl
     * @return string
     */
    public function car_model_add(){
        //设置查询条件
        $car_brand_list = ICarBrandCache::getList();

        //页面赋值
        $this->assign("car_brand_list",$car_brand_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑车辆型号
     * @开发者：cxl
     * @return string
     */
    public function car_model_edit(){
        //接收参数
        $car_model_id = IRequest::get('car_model_id',IRequest::NOT_EMPTY,"车辆品牌型号编号不能为空！");

        //设置查询条件
        $car_brand_list = ICarBrandCache::getList();

        //设置查询条件
        $car_model_info = ICarModelCache::getInfo($car_model_id);
        if(empty($car_model_info['car_brand_id'])){
            error("取得车辆品牌型号信息失败！");
        }

        //页面赋值
        $this->assign("car_model_id",$car_model_id);
        $this->assign("car_brand_list",$car_brand_list);
        $this->assign("car_model_info",$car_model_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑车辆型号提交
     * 开发者：cxl
     */
    public function car_model_submit(){
        //接收参数
        $car_model_id = IRequest::get("car_model_id");
        $car_brand_id = IRequest::get("car_brand_id",IRequest::NOT_EMPTY,"车辆品牌编号不能为空！");
        $car_model_name = IRequest::get("car_model_name",IRequest::NOT_EMPTY,"车辆品牌型号名称不能为空！");
        $car_model_url = IRequest::get("car_model_url");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        //判断数据提交类型
        if(empty($car_model_id)){
            //判断是否存在
            $car_model_where['car_brand_id'] = $car_brand_id;
            $car_model_where['car_model_name'] = $car_model_name;
            $car_model_count = IDb::getInstance("asset_car_model")->setDbWhere($car_model_where)->con();
            if($car_model_count > 0){
                error("您输入的品牌型号已经存在！");
            }

            //新增保存
            $car_model_data['car_brand_id'] = $car_brand_id;
            $car_model_data['car_model_name'] = $car_model_name;
            $car_model_data['car_model_url'] = $car_model_url;
            $car_model_data['order_by'] = $order_by;
            $car_model_data['remark'] = $remark;
            $car_model_data['create_admin'] = get_login_admin_id();
            $car_model_data['create_time'] = get_date_time();
            $car_model_add = IDb::getInstance("asset_car_model")->setDbData($car_model_data)->add();
            if($car_model_add === false){
                error("创建车辆品牌型号失败！");
            }
        }else{
            //修改保存
            $car_model_data['car_brand_id'] = $car_brand_id;
            $car_model_data['car_model_name'] = $car_model_name;
            $car_model_data['car_model_url'] = $car_model_url;
            $car_model_data['order_by'] = $order_by;
            $car_model_data['remark'] = $remark;
            $car_model_data['update_admin'] = get_login_admin_id();
            $car_model_data['update_time'] = get_date_time();
            $car_model_where['car_model_id'] = $car_model_id;
            $car_model_upd = IDb::getInstance("asset_car_model")->setDbWhere($car_model_where)->setDbData($car_model_data)->upd();
            if($car_model_upd === false){
                error("修改车辆品牌型号失败！");
            }

            //清除缓存
            ICarModelCache::deleteInfo($car_model_id);
        }

        //清除缓存
        ICarModelCache::deleteList($car_brand_id);

        //画面跳转
        $this->redirect("car_model_list");
    }

    /**
     * @功能：删除汽车品牌型号
     * @开发者：cxl
     * @return string
     */
    public function car_model_del(){
        //接收参数
        $car_model_id = IRequest::get('car_model_id',IRequest::NOT_EMPTY,"车辆品牌型号编号不能为空！");

        //修改管理员信息
        $car_model_where['car_model_id'] = $car_model_id;
        $car_model_data['is_delete'] = IsDelete::Yes;
        $car_model_data['update_admin'] = get_login_admin_id();
        $car_model_data['update_time'] = get_date_time();
        $car_model_upd = IDb::getInstance('asset_car_model')->setDbWhere($car_model_where)->setDbData($car_model_data)->upd();
        if($car_model_upd === false){
            error("删除汽车品牌型号失败！");
        }

        //取得缓存信息
        $car_model_info = ICarModelCache::getInfo($car_model_id);

        //清除缓存
        ICarModelCache::deleteInfo($car_model_id);
        ICarModelCache::deleteList($car_model_info['car_brand_id']);

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：车辆品牌型号还原
     * @开发者：cxl
     * @return string
     */
    public function car_model_recycle(){
        //接收参数
        $car_model_id = IRequest::get('car_model_id',IRequest::NOT_EMPTY,"车辆品牌型号编号不能为空！");

        //修改管理员信息
        $car_model_where['car_model_id'] = $car_model_id;
        $car_model_data['is_delete'] = IsDelete::No;
        $car_model_data['update_admin'] = get_login_admin_id();
        $car_model_data['update_time'] = get_date_time();
        $car_model_upd = IDb::getInstance('asset_car_model')->setDbWhere($car_model_where)->setDbData($car_model_data)->upd();
        if($car_model_upd === false){
            error("车辆品牌型号还原失败！");
        }

        //取得缓存信息
        $car_model_info = ICarModelCache::getInfo($car_model_id);

        //清除缓存
        ICarModelCache::deleteInfo($car_model_id);
        ICarModelCache::deleteList($car_model_info['car_brand_id']);

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：车辆配置列表
     * @开发者：cxl
     * @return string
     */
    public function car_config_list(){
        //接收参数
        $car_brand_id = IRequest::get('car_brand_id');
        $car_model_id = IRequest::get('car_model_id');
        $car_config_id = IRequest::get('car_config_id');
        $car_config_name = IRequest::get('car_config_name');

        //设置查询条件
        $car_brand_list = ICarBrandCache::getList();

        //设置查询条件
        $car_model_where['is_delete'] = IsDelete::No;
        $car_model_list = IDb::getInstance('asset_car_model')->setDbFiled("car_model_id,car_brand_id,car_model_name")->setDbWhere($car_model_where)->setDbOrder("car_model_id")->sel();

        //设置查询条件
        $car_config_where = null;
        if(!empty($car_brand_id)) $car_config_where['car_brand_id'] = $car_brand_id;
        if(!empty($car_model_id)) $car_config_where['car_model_id'] = $car_model_id;
        if(!empty($car_config_id)) $car_config_where['car_config_id'] = $car_config_id;
        if(!empty($car_config_name)) $car_config_where['car_config_name'] = array("like","%{$car_config_name}%");
        $car_config_list = IDb::getInstance('asset_car_config')
            ->setDbWhere($car_config_where)
            ->setDbOrder("car_config_id")
            ->pag();

        //页面赋值
        $this->assign("car_brand_id",$car_brand_id);
        $this->assign("car_model_id",$car_model_id);
        $this->assign("car_config_id",$car_config_id);
        $this->assign("car_config_name",$car_config_name);
        $this->assign("car_brand_list",$car_brand_list);
        $this->assign("car_model_list",$car_model_list);
        $this->assign("car_config_list",$car_config_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：车辆配置详情
     * @开发者：cxl
     * @return string
     */
    public function car_config_info(){
        //接收参数
        $car_config_id = IRequest::get('car_config_id',IRequest::NOT_EMPTY,"车辆配置编号不能为空！");

        //设置查询条件
        $car_config_info = ICarConfigCache::getInfo($car_config_id);
        if(empty($car_config_info['car_model_id'])){
            error("取得汽车配置信息失败！");
        }

        //设置查询条件
        $car_model_info = ICarModelCache::getInfo($car_config_info['car_model_id']);
        if(empty($car_model_info['car_brand_id'])){
            error("取得汽车配置信息失败！");
        }

        //设置查询条件
        $car_brand_info = ICarBrandCache::getInfo($car_model_info['car_brand_id']);
        if(empty($car_brand_info['car_brand_id'])){
            error("取得汽车配置信息失败2！");
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("car_brand_info",$car_brand_info);
        $this->assign("car_model_info",$car_model_info);
        $this->assign("car_config_info",$car_config_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加车辆配置
     * @开发者：cxl
     * @return string
     */
    public function car_config_add(){
        //设置查询条件
        $car_brand_list = ICarBrandCache::getList();

        //设置查询条件
        $car_model_where['is_delete'] = IsDelete::No;
        $car_model_list = IDb::getInstance('asset_car_model')->setDbFiled("car_model_id,car_brand_id,car_model_name")->setDbWhere($car_model_where)->setDbOrder("car_model_id")->sel();

        //页面赋值
        $this->assign("car_brand_list",$car_brand_list);
        $this->assign("car_model_list",$car_model_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑车辆配置
     * @开发者：cxl
     * @return string
     */
    public function car_config_edit(){
        //接收参数
        $car_config_id = IRequest::get('car_config_id',IRequest::NOT_EMPTY,"车辆配置编号不能为空！");

        //设置查询条件
        $car_brand_list = ICarBrandCache::getList();

        //设置查询条件
        $car_model_where['is_delete'] = IsDelete::No;
        $car_model_list = IDb::getInstance('asset_car_model')->setDbFiled("car_model_id,car_brand_id,car_model_name")->setDbWhere($car_model_where)->setDbOrder("car_model_id")->sel();

        //设置查询条件
        $car_config_info = ICarConfigCache::getInfo($car_config_id);
        if(empty($car_config_info['car_brand_id'])){
            error("取得车辆配置信息失败！");
        }

        //页面赋值
        $this->assign("car_config_id",$car_config_id);
        $this->assign("car_brand_list",$car_brand_list);
        $this->assign("car_model_list",$car_model_list);
        $this->assign("car_config_info",$car_config_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑车辆配置提交
     * 开发者：cxl
     */
    public function car_config_submit(){
        //接收参数
        $car_config_id = IRequest::get("car_config_id");
        $car_brand_id = IRequest::get("car_brand_id",IRequest::NOT_EMPTY,"车辆品牌编号不能为空！");
        $car_model_id = IRequest::get("car_model_id",IRequest::NOT_EMPTY,"车辆型号编号不能为空！");
        $car_config_name = IRequest::get("car_config_name",IRequest::NOT_EMPTY,"车辆配置名称不能为空！");
        $car_config_url = IRequest::get("car_config_url");
        $car_price = IRequest::get("car_price",IRequest::NOT_EMPTY,"车辆配置价格不能为空！");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        //判断数据提交类型
        if(empty($car_config_id)){
            //判断是否存在
            $car_config_where['car_brand_id'] = $car_brand_id;
            $car_config_where['car_model_id'] = $car_model_id;
            $car_config_where['car_config_name'] = $car_config_name;
            $car_config_info = IDb::getInstance("asset_car_config")->setDbWhere($car_config_where)->row();
            if(!empty($car_config_info)){
                error("您输入的配置已经存在！");
            }

            //新增保存
            $car_config_data['car_brand_id'] = $car_brand_id;
            $car_config_data['car_model_id'] = $car_model_id;
            $car_config_data['car_config_name'] = $car_config_name;
            $car_config_data['car_config_url'] = $car_config_url;
            $car_config_data['car_price'] = $car_price;
            $car_config_data['order_by'] = $order_by;
            $car_config_data['remark'] = $remark;
            $car_config_data['create_admin'] = get_login_admin_id();
            $car_config_data['create_time'] = get_date_time();
            $car_config_add = IDb::getInstance("asset_car_config")->setDbData($car_config_data)->add();
            if($car_config_add === false){
                error("创建车辆配置失败！");
            }
        }else{
            //修改保存
            $car_config_data['car_brand_id'] = $car_brand_id;
            $car_config_data['car_model_id'] = $car_model_id;
            $car_config_data['car_config_name'] = $car_config_name;
            $car_config_data['car_config_url'] = $car_config_url;
            $car_config_data['car_price'] = $car_price;
            $car_config_data['order_by'] = $order_by;
            $car_config_data['remark'] = $remark;
            $car_config_data['update_admin'] = get_login_admin_id();
            $car_config_data['update_time'] = get_date_time();
            $car_config_where['car_config_id'] = $car_config_id;
            $car_config_upd = IDb::getInstance("asset_car_config")->setDbWhere($car_config_where)->setDbData($car_config_data)->upd();
            if($car_config_upd === false){
                error("修改车辆配置失败！");
            }

            //清除缓存
            ICarConfigCache::deleteInfo($car_config_id);
        }

        //清除缓存
        ICarConfigCache::deleteList($car_brand_id,$car_model_id);

        //画面跳转
        $this->redirect("car_config_list");
    }

    /**
     * @功能：删除汽车配置
     * @开发者：cxl
     * @return string
     */
    public function car_config_del(){
        //接收参数
        $car_config_id = IRequest::get('car_config_id',IRequest::NOT_EMPTY,"车辆配置编号不能为空！");

        //修改管理员信息
        $car_config_where['car_config_id'] = $car_config_id;
        $car_config_data['is_delete'] = IsDelete::Yes;
        $car_config_data['update_admin'] = get_login_admin_id();
        $car_config_data['update_time'] = get_date_time();
        $car_config_upd = IDb::getInstance('asset_car_config')->setDbWhere($car_config_where)->setDbData($car_config_data)->upd();
        if($car_config_upd === false){
            error("删除汽车配置失败！");
        }

        //取得缓存信息
        $car_config_info = ICarConfigCache::getInfo($car_config_id);

        //清除缓存
        ICarConfigCache::deleteInfo($car_config_id);
        ICarConfigCache::deleteList($car_config_info['car_brand_id'],$car_config_info['car_model_id']);

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：车辆配置还原
     * @开发者：cxl
     * @return string
     */
    public function car_config_recycle(){
        //接收参数
        $car_config_id = IRequest::get('car_config_id',IRequest::NOT_EMPTY,"车辆配置编号不能为空！");

        //修改管理员信息
        $car_config_where['car_config_id'] = $car_config_id;
        $car_config_data['is_delete'] = IsDelete::No;
        $car_config_data['update_admin'] = get_login_admin_id();
        $car_config_data['update_time'] = get_date_time();
        $car_config_upd = IDb::getInstance('asset_car_config')->setDbWhere($car_config_where)->setDbData($car_config_data)->upd();
        if($car_config_upd === false){
            error("车辆配置还原失败！");
        }

        //取得缓存信息
        $car_config_info = ICarConfigCache::getInfo($car_config_id);

        //清除缓存
        ICarConfigCache::deleteInfo($car_config_id);
        ICarConfigCache::deleteList($car_config_info['car_brand_id'],$car_config_info['car_model_id']);

        //画面跳转
        $this->success("操作成功！");
    }
}
